package chapter04;

public class jiayoustation {
    public static void main(String[] args) {
        int[] gas = {1, 2, 3, 4, 5};
        int[] cost = {3, 4, 5, 1, 2};
        System.out.println(sst(gas, cost));
    }

    public static int sst(int[] gas, int[] cost) {
        int allgas = 0;//总油量
        int allcost = 0;//总消耗量
        for (int i = 0; i < gas.length; i++) {
            allcost += cost[i];
            allgas += gas[i];
        }
        if (allgas < allcost) {
            return -1;
        }
        int start = 0;//加油站起点
        int oil = 0;//当前汽油量
        for (int i = 0; i < gas.length; i++) {
            oil = oil - cost[i] + gas[i];
            if (oil < 0) {
                oil = 0;
                start = i + 1;
            }
        }

        return start;
    }
}
//}
//class Solution {
//    public int canCompleteCircuit(int[] gas, int[] cost) {
//        int n = gas.length;
//        int sum = 0;
//        for(int i = 0;i < n;i++){
//            sum += gas[i] - cost[i];
//        }
//
//        if(sum < 0){
//            return -1;
//        }
//
//        int currentGas = 0;
//        int start = 0;
//        for(int i = 0;i < n;i++){
//            currentGas += gas[i] - cost[i];
//            if(currentGas < 0){
//                currentGas = 0;
//                start = i + 1;
//            }
//        }
//        return start;
//    }
//}
